using System.Collections.Generic;
using System.Linq;

namespace HtmlUtilities.UITest
{
    public static class ElementCommands
    {
        public static IEnumerable<Command<string>> GetCommands()
        {
            return new List<Command<string>>
                       {
                           new Command<string>("AppendChild", AppendChild),
                           new Command<string>("AppendChildRelative", AppendChildRelative),
                           new Command<string>("Children", () => "TestDiv has " + TestDiv.Children.Count + " Children"),
                           new Command<string>("CssClass", () => TestDiv.CssClass),
                           new Command<string>("Focus", Focus),
                           new Command<string>("GetAttribute", () => TestDiv.GetAttribute("lang")),
                           new Command<string>("GetSetStyle", GetSetStyle),
                           new Command<string>("Id", () => TestDiv.Id),
                           new Command<string>("Parent", () => TestDiv.Parent.Id),
                           new Command<string>("RemoveAttribute", RemoveAttribute),
                           new Command<string>("RemoveChild", RemoveChild),
                           new Command<string>("RemoveStyle", RemoveStyle),
                           new Command<string>("SetAttribute", () => { TestDiv.SetAttribute("lang", "fr"); return "Lang set"; }),
                           new Command<string>("TagName", () => TestDiv.TagName)
                       };
        }

        private static IHtmlElement TestDiv { get { return Html.Page.Document.GetElementById("testing"); } }

        private static string RemoveStyle()
        {
            TestDiv.RemoveStyleAttribute("border");
            return "Border Style: " + TestDiv.GetStyleAttribute("border");
        }

        private static string RemoveChild()
        {
            var ul = Html.Page.Document.CreateElement("ul");
            TestDiv.AppendChild(ul);

            var items = CreateLIs("One", "Two", "Three");

            ul.AppendChild(items[0]);
            ul.AppendChild(items[1]);
            ul.AppendChild(items[2]);

            ul.RemoveChild(items[1]);

            return "Added LIs but removed one";
        }

        private static string RemoveAttribute()
        {
            TestDiv.RemoveAttribute("lang");
            return "TestDiv Lang: " + TestDiv.GetAttribute("lang");
        }

        private static string GetSetStyle()
        {
            TestDiv.SetStyleAttribute("border", "thin dotted");
            return TestDiv.GetStyleAttribute("border");
        }

        private static string Focus()
        {
            var input = Html.Page.Document.CreateElement("input");
            TestDiv.AppendChild(input);
            input.Focus();

            return "New input got focus";
        }

        private static IHtmlElement[] CreateLIs(params string[] values)
        {
            return values.Select(value => CreateLI(value)).ToArray();
        }

        private static string AppendChildRelative()
        {
            var ul = Html.Page.Document.CreateElement("ul");
            TestDiv.AppendChild(ul);

            var items = CreateLIs("One", "Two", "Three", "Inserted");

            ul.AppendChild(items[0]);
            ul.AppendChild(items[1]);
            ul.AppendChild(items[2]);
            ul.AppendChild(items[3], items[1]);

            return "Adding item relative";
        }

        private static string AppendChild()
        {
            TestDiv.AppendChild(ImageOfBrian());
            return "Adding Image of Brian";
        }

        private static IHtmlElement ImageOfBrian()
        {
            var childToAppend = Html.Page.Document.CreateElement("img");
            childToAppend.SetProperty("src", "http://s3.amazonaws.com/twitter_production/profile_images/71633894/BrianGenisio.jpg");
            return childToAppend;
        }

        private static IHtmlElement CreateLI(string text)
        {
            var li = Html.Page.Document.CreateElement("li");
            li.SetProperty("innerHTML", text);
            return li;
        }
    }
}